<%@ page language="java" pageEncoding="UTF-8" %>
<%@ include file="../portal_common/taglibs.jsp" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>设置-个人信息</title>
    <link rel="icon" href="assets/img/favicon.ico">

    <link rel="stylesheet" type="text/css" href="${ctx}/css/webbase.css"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/css/pages-seckillOrder.css"/>
</head>

<body>
<!-- 头部栏位 -->
<!--页面顶部-->
<div id="nav-bottom">
    <!--顶部-->
    <%@include file="../portal_common/top.jsp" %>
</div>

<script type="text/javascript" src="${ctx}/js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#service").hover(function () {
            $(".service").show();
        }, function () {
            $(".service").hide();
        });
        $("#shopcar").hover(function () {
            $("#shopcarlist").show();
        }, function () {
            $("#shopcarlist").hide();
        });

    })
</script>
<script type="text/javascript" src="${ctx}/js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="${ctx}/js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="${ctx}/js/plugins/jquery-placeholder/jquery.placeholder.min.js"></script>
<script type="text/javascript" src="${ctx}/js/widget/nav.js"></script>
<script type="text/javascript" src="${ctx}/pages/userInfo/distpicker.data.js"></script>
<script type="text/javascript" src="${ctx}/pages/userInfo/distpicker.js"></script>
<script type="text/javascript" src="${ctx}/pages/userInfo/main.js"></script>
</body>
<!--header-->
<div id="account">
    <div class="py-container">
        <div class="yui3-g home">
            <!--左侧列表-->
            <div class="yui3-u-1-6 list">
                <div class="person-info">
                    <div class="person-photo">
                        <c:if test="${headPic == null}">
                            <img src="${ctx}/img/_/photo.png" alt="">
                        </c:if>
                        <c:if test="${headPic != null}">
                            <img width="60px" height="60px" src="${headPic}" alt="">
                        </c:if>
                    </div>
                    <div class="person-account">
                        <span class="name">${username}</span>
                    </div>
                    <div class="clearfix"></div>
                </div>
                <div class="list-items">
                    <dl>
                        <dt><i>·</i> 订单中心</dt>
                        <dd><a href="${ctx}/order/list">我的订单</a></dd>
                        <dd><a href="${ctx}/order/list?status=1">待付款</a></dd>
                        <dd><a href="${ctx}/order/list?status=3">待发货</a></dd>
                        <dd><a href="${ctx}/order/list?status=4">待收货</a></dd>
                        <dd><a href="${ctx}/order/list?status=6">待评价</a></dd>
                        <dd><a href="${ctx}/order/list?status=2">交易关闭</a></dd>
                    </dl>
                    <dl>
                        <dt><i>·</i> 设置</dt>
                        <dd><a href="${ctx}/user/details">个人信息</a></dd>
                        <dd><a href="${ctx}/address/list" class="list-active">地址管理</a></dd>
                    </dl>
                </div>
            </div>
            <!--右侧主内容-->
            <div class="yui3-u-5-6">
                <div class="body userAddress">
                    <div class="address-title">
                        <span class="title">地址管理</span>
                        <a data-toggle="modal" data-target=".edit" data-keyboard="false"
                           class="sui-btn  btn-info add-new">添加新地址</a>
                        <span class="clearfix"></span>
                    </div>
                    <div class="address-detail">
                        <table class="sui-table table-bordered">
                            <thead>
                            <tr>
                                <th>姓名</th>
                                <th>地址</th>
                                <th>联系电话</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${list}" var="row" varStatus="status">
                                <tr>
                                    <td>${row.contact}</td>
                                    <td>${row.address}</td>
                                    <td>${row.mobile}</td>
                                    <td>
                                        <%--<a href="#" onclick="editAddress('${status.index}')">编辑</a>--%>
                                        <a href="#" id="delAddress" onclick="delAddress('${row.id}')">删除</a>
                                        <c:if test="${row.isDefault == 1}">
                                            默认地址
                                        </c:if>
                                        <c:if test="${row.isDefault == 0}">
                                            <a href="#" id="setDefault" onclick="setDefault('${row.id}')">设为默认</a>
                                        </c:if>
                                    </td>
                                </tr>
                            </c:forEach>
                            </tbody>
                        </table>
                    </div>
                    <!--新增地址弹出层-->
                    <div tabindex="-1" id="modelAdd" role="dialog" data-hasfoot="false" class="sui-modal hide fade edit"
                         style="width:580px;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" data-dismiss="modal" aria-hidden="true" class="sui-close">×
                                    </button>
                                    <h4 id="myModalLabel" class="modal-title">新增地址</h4>
                                </div>
                                <div class="modal-body">
                                    <form action="" id="addressForm" class="sui-form form-horizontal">
                                        <div class="control-group">
                                            <label class="control-label">收货人：</label>
                                            <div class="controls">
                                                <input name="contact" type="text" class="input-medium">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">详细地址：</label>
                                            <div class="controls">
                                                <input type="text" class="input-large" name="address">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">联系电话：</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" name="mobile">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">地址别名：</label>
                                            <div class="controls">
                                                <input type="text" name="alias" class="input-medium">
                                            </div>
                                            <div class="othername">
                                                建议填写常用地址：<a href="#" class="sui-btn btn-default">家里</a>　
                                                <a href="#" class="sui-btn btn-default">父母家</a>
                                                　<a href="#" class="sui-btn btn-default">公司</a>
                                            </div>
                                        </div>

                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" data-ok="modal" onclick="addAddress()"
                                            class="sui-btn btn-primary btn-large">确定
                                    </button>
                                    <button type="button" data-dismiss="modal" class="sui-btn btn-default btn-large">
                                        取消
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var addressList = [
        <c:forEach items="${list}" var="row">
        {
            id:${row.id},
            title: '${row.contact}',
            alias: '${row.alias}',
            address: '${row.address}'
        },
        </c:forEach>];

    var element;
    function editAddress(index) {
        console.log(index);
        element = addressList[index];
        console.log(element);
        $('#modelAdd');
    }

    function addAddress() {
        $.ajax({
                url: "${ctx}/address/add",
                type: 'post',
                data: $('#addressForm').serialize(),
                success: function (response) {
                    console.log(response);
                    if (response.success) {
                        alert(response.message);
                        window.location.reload();
                    } else {
                        alert(response.message);
                    }
                }
            }
        );
    }

    function delAddress(id) {
        $.ajax({
                url: "${ctx}/address/deleteById",
                type: 'post',
                data: {
                    'id': id
                },
                dataType: 'json',
                success: function (response) {
                    console.log(response);
                    if (response.success) {
                        alert(response.message);
                        window.location.reload();
                    } else {
                        alert(response.message);
                    }
                }
            }
        );
    }

    function setDefault(id) {
        $.ajax({
                url: "${ctx}/address/setDefault",
                type: 'post',
                data: {
                    'id': id
                },
                dataType: 'json',
                success: function (response) {
                    console.log(response);
                    if (response.success) {
                        alert(response.message);
                        window.location.reload();
                    } else {
                        alert(response.message);
                    }
                }
            }
        );
    }
</script>
<!-- 底部栏位 -->
<!--页面底部-->
<%@include file="../portal_common/foot.jsp" %>
<!--页面底部END-->

</html>